import { getPropsList, resetAttr } from "../attr"
import { selectModeOptions } from "../../lib/const"
import { getTableOption } from "../../lib/table"
import { getAllAnalysisComponentByEditType, getChartAxisData,updateAnalysisComponent } from "../../lib/analysisData"
import EventBus from "../../EventBus"
import store from '@/store'
import { getComponentByKey, getComponentListByKeys, getComponentListByTypes } from "../../lib/utils"

const attr = ({selectFilterFieldList = [], selectFilterFieldOptions = []}) => [
  {
    type: "input",
    label: "标题",
    fieldName: "title",
    extraConfig: {
      className: "row",
    },
    dataType: String,
  },

  {
    type: "multiple-field-select",// "multiple-field-select",
    label: "筛选字段",
    fieldName: "filterField",
    extraConfig: {
      className: "row",
      mode: "multiple"
    },
    options: selectFilterFieldList,
    dataType: Array,
      onChange: (fieldName, value) => {
        const {
          canvasComponentList,
          selectedComponent // 当前绑定的过滤组件
        } = store.state.lowCode
        // 设置最新值
        // selectedComponent.props[fieldName] = value

        console.log(fieldName, value)
        // 拿到当前选中的字段的数据,也就是携带的parentId(绑定的业务组件id)
        // const analysisComponentKey = option.data.attrs['data-parent-id']
        const analysisComponentKeys = value.map(item => item.split('/')[0])
        const paramsList = []
        // 获取所有业务组件
        const analysisComponentList = getAllAnalysisComponentByEditType(true)
        // 遍历所有业务组件
        analysisComponentList.forEach((analysisComponent,dataIndex) => {
          const index = analysisComponent.props.filterKeys.indexOf(selectedComponent.key)
          // 如果当前下拉框,选择了当前组件的某个字段
          if (value.find(item => item.split('/')[0] === analysisComponent.key)){
            // 如果当前业务组件已经绑定了该过滤组件,就不处理,如果未绑定,则添加
            if (index === -1){
              analysisComponent.props.filterKeys.push(selectedComponent.key)
            }
          }
          // 如果未选择
          else {
            // 如果当前业务组件已经绑定了该过滤组件,则删除该业务组件与下拉框的绑定,不过未绑定,则不处理
            if (index > -1){
              analysisComponent.props.filterKeys.splice(index, 1)
            }
          }


          if (analysisComponentKeys.includes(analysisComponent.key)){
            // 更新该业务组件的options
            const {
              databaseName, datasourceId, gradeThematicId, tableName,chartTypeId
            } = getChartAxisData(analysisComponent.props.value).dataSource
            const params = {
              "dataSourceDetail": {
                databaseName,
                datasourceId,
                gradeThematicId,
                tableName
              },
              "pageNum": 1,
              "pageSize": 10
            }
            paramsList.push(params)
          }
        })
        console.log(paramsList)

        if (value.length > 0){
          const columnName = value[0].split('/')[1]
          // 单维度
          Promise.all(paramsList.map(params => getTableOption(3,columnName,params)))
          .then(data => {
            const options = []
            data.forEach(item => {
              item.forEach(obj => {
                if (!options.find(option => option.value === obj.value)){
                  options.push(obj)
                }
              })
            })
            selectedComponent.props.options = options
            // 设置初始值
            resetAttr(store, fieldName, value)
          })
        }
        else {
          selectedComponent.props.options = []
          // 清空初始值
          resetAttr(store, fieldName, value)
        }
    },
  },
  {
    type: "select",
    label: "默认值",
    fieldName: "value",
    options: store.state.lowCode.selectedComponent.props.options,
    extraConfig: {
      className: "row",
      // multiple:  true, // multiple: vm.$store.state.lowCode.selectedComponent.props.multiple,
      mode: store.state.lowCode.selectedComponent.props.mode,
    },
    dataType: String | Number | Array,
  },
  // {
  //   type: "select-group",
  //   label: "筛选字段",
  //   fieldName: "filterField",
  //   options: selectFilterFieldList,
  //   extraConfig: {
  //     className: "row",
  //   },
  //   onChange: (fieldName, value, option)=> {

  //     // 拿到当前选中的字段的数据,也就是携带的parentId(绑定的业务组件id)
  //     const analysisComponentKey = option.data.attrs['data-parent-id']
  //     // TODO:
  //     const {
  //       selectFilterFieldList,
  //       filterData,
  //       canvasComponentList,
  //       selectedComponent // 当前绑定的过滤组件
  //     } = store.state.lowCode
  //     // 设置最新值
  //     selectedComponent.props[fieldName] = value

  //     // 当前绑定的业务组件
  //     const analysisComponent = getComponentByKey(canvasComponentList,analysisComponentKey)
  //     const index = analysisComponent.props.filterKeys.indexOf(selectedComponent.key)
  //     if(index > -1){
  //       analysisComponent.props.filterKeys.splice(index, 1, selectedComponent.key)
  //     }
  //     else {
  //       analysisComponent.props.filterKeys.push(selectedComponent.key)
  //     }

  //     const {
  //       databaseName, datasourceId, gradeThematicId, tableName,chartTypeId
  //     } = getChartAxisData(analysisComponent.props.value).dataSource
  //     const params = {
  //       "dataSourceDetail": {
  //         databaseName,
  //         datasourceId,
  //         gradeThematicId,
  //         tableName
  //       },
  //       "pageNum": 1,
  //       "pageSize": 10
  //     }
  //     // 单维度
  //     // getTableOption(chartTypeId,fieldName,params)
  //     getTableOption(3,value,params)
  //     .then(data => {
  //       // store.commit("lowCode/setSelectFilterFieldOptions", data)
  //       selectedComponent.props.options = data
  //       // 清空初始值
  //       resetAttr(store, fieldName, value)
  //     })
  //   },
  //   dataType: String,
  // },
  {
    type: "select",
    label: "多选",
    fieldName: "mode",
    options: selectModeOptions,
    extraConfig: {
      className: "row",
    },
    onChange: (fieldName, value) => {
      // 清空初始值
      resetAttr(store, fieldName, value, [])
    },
    dataType: String,
  }
]
export function getProps(data){
  return getPropsList(attr(data = {}))
}

export default {
  attr,
  // isNoCommon: true
}


